package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.query.RegionQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by LENOVO on 2019/8/10.
 */
@Service
public class DestinationServiceImpl implements IDestinationService {
    @Autowired
    private DestinationMapper destinationMapper;
    @Autowired
    private RegionMapper regionMapper;


    public List<Destination> getDestByDeep(int deep) {

        return destinationMapper.selectDestsByDeep(deep);
    }

    @Override
    public List<Destination> getDestByRegionId(Long rid) {
        //需要执行一个分支: 国内分支
        if (rid == -1){
            return destinationMapper.selectSubDestsByParendId(1L);
        }
        Region region = regionMapper.selectByPrimaryKey(rid);
        //某个区域下关联的目的地id
        Long[] refIds = region.getRefIds();
        return destinationMapper.selectDestByIds(refIds);
    }


    //分页
    @Override
    public PageInfo<Destination> query(DestinationQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        return new PageInfo(destinationMapper.selectForList(qo));
    }

    @Override
    public List<Destination> getToasts(Long parentId) {
        ArrayList<Destination> list = new ArrayList<>();
        createToast(parentId,list);
        Collections.reverse(list);//列表反序
        return list;
    }

    @Override
    public void changeHotValue(int hot, Long id) {
        destinationMapper.updateByHotAndId(hot,id);
    }

    @Override
    public void delete(Long id) {
        destinationMapper.deleteByPrimaryKey(id);
    }


    //获取国家
    @Override
    public Destination getCountry(Long id) {
        List<Destination> list = this.getToasts(id);
        if(list !=null && list.size()>0){
            return  list.get(0);
        }
        return null;
    }
    //获取省份
    @Override
    public Destination getProvince(Long id) {
        //必须是国内才设置省份
        List<Destination> list = this.getToasts(id);
        if(list !=null &&list.size()>0){
            //国外
            if(list.get(0).getId() !=1){
                return null;
            }
        }
        if (list.size()>1){
            return list.get(1);
        }

        return null;
    }

    @Override
    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    private void createToast(Long parentId,List<Destination> list){
        //通过父类的id,查询父类
        Destination destination = destinationMapper.selectByPrimaryKey(parentId);
        if(destination ==null){
            return;
        }
        //把查询到父类添加到数组
        list.add(destination);
        if (destination.getParent() !=null){
            createToast(destination.getParent().getId(),list);
        }

    }
}
